<?php

namespace app\api\controller;

use cmf\controller\HomeBaseController;

class GuestbookController extends HomeBaseController{
    
    protected $guestbook_model;
    
    public function _initialize() {
        parent::_initialize();
    }
    
    // 留言提交
    public function addmsg(){
        $captcha = $this->request->param('captcha');
        if(!cmf_captcha_check($captcha)){
            $this->error("验证码错误！");
        }
        
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];

            // $result = $this->validate($post, 'Guestbook');
            // if ($result !== true) {
            //     $this->error($result);
            // }


            $post['createtime'] = time();
            $res = db('guestbook')->insert($post);
            if ($res !== false) {
                
                $content = '';
                $content .= '姓名：'.$post['full_name'].'<br>';
                $content .= '联系电话：'.$post['phone'].'<br>';
                $content .= '电子邮箱：'.$post['email'].'<br>';
                $content .= '公司名称：'.$post['company'].'<br>';
                $content .= '联系地址：'.$post['address'].'<br>';
                $content .= '传真号码：'.$post['fax'].'<br>';
                $content .= '其它说明：'.$post['msg'];

                $smtpSetting = cmf_get_option('smtp_setting');
                // 发送邮件
                cmf_send_email($smtpSetting['recipient_mailbox'], $post['full_name'].'-投资资讯', $content);

                $this->success("留言成功！");
            } else {
                $this->error("留言失败！");
            }
        }
        
    }
}
